package pageTest.login;

import constants.HostConstant;
import constants.UserInfoConstant;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import utils.WebDriverUtils;

public class TeamPage extends WebDriverUtils {
    private static final String TEAM_PAGE_URL = "http://" + HostConstant.HOST + ":5173/team";

    public TeamPage() {
        super(INIT_URL);
    }


    @BeforeClass
    public void setUp() {
        driver.get(super.LOGIN_PAGE_URL);
        driver.findElement(By.cssSelector("#van-field-1-input")).sendKeys(UserInfoConstant.USER_ACCOUNT);
        driver.findElement(By.cssSelector("#van-field-2-input")).sendKeys(UserInfoConstant.USER_PASSWORD);
        driver.findElement(By.cssSelector("#login > form > div:nth-child(2) > button.van-button.van-button--primary.van-button--normal.van-button--block.van-button--round")).click();
        // 确保登录成功了(登录操作响应已经返回， cookie 被种上)
        wait.until(ExpectedConditions.textToBe(By.cssSelector("#app > div:nth-child(1) > div > div > div > div.van-nav-bar__title.van-ellipsis"), "CAMPUS 伙伴匹配"));
    }

    @BeforeMethod
    public void beforeEachTest() {
        driver.get(TEAM_PAGE_URL);
    }


//    @Test
    public void joinTeam() {
        driver.findElement(By.cssSelector("#teamPage > div.van-pull-refresh > div > div.van-list > div:nth-child(1) > div.van-card__footer > button > div > span")).click();
    }

    @Test
    public void createTeam() {
        // 点击 “+” 按钮，跳转到创建队伍页面
        String topNavBarSlt = "#app > div:nth-child(1) > div > div > div > div.van-nav-bar__title.van-ellipsis";
        driver.findElement(By.cssSelector("#teamPage > button")).click();
        wait.until(ExpectedConditions.textToBe(By.cssSelector(topNavBarSlt), "创建队伍"));
        String inputTeamName = "这是一个自动化测试";
        String inputTeamDesc = "这是自动化测试队伍描述";
        driver.findElement(By.tagName("input")).sendKeys(inputTeamName);
        driver.findElement(By.tagName("textarea")).sendKeys(inputTeamDesc);
        driver.findElement(By.cssSelector("#teamAddPage > form > div:nth-child(2) > button")).click();
        // 自动跳转到队伍页面, 验证队伍是否创建成功
        wait.until(ExpectedConditions.textToBe(By.cssSelector(topNavBarSlt), "队伍"));
        String teamName = driver.findElement(By.cssSelector("#teamPage > div.van-pull-refresh > div > div.van-list > div:nth-child(1) > div.van-card__header > div > div:nth-child(1) > div.team-title")).getText();
        String teamDesc = driver.findElement(By.cssSelector("#teamPage > div.van-pull-refresh > div > div.van-list > div:nth-child(1) > div.van-card__header > div > div:nth-child(1) > div.van-card__desc.van-ellipsis")).getText();
        assert inputTeamName.equals(teamName);
        assert inputTeamDesc.equals(teamDesc);
    }

    @AfterClass
    public void tearDown() {
        driver.quit();
    }
}
